<?php

class Experiment {
	
	var $lisa;
	var $seed_entities;
	var $thresholds;
	var $nos_load_iterations = 20;
	var $fanout_ratio = 0.2;
	var $stats;
	var $load_stats;
	var $nos_cols;
	
	
    function __construct($lisa=false) {
		if ( $lisa ) {
			$this->lisa = $lisa;
		} else {
        	$this->lisa = new LargeScaleSpreadingActivation();
			$this->lisa->set_nos_internal_cycles(1);
		}
	}

	function run($seed_entities, $thresholds) {
		$this->seed_entities = $seed_entities;
		$this->thresholds = $thresholds;
		$this->clear_stats();
		foreach($this->thresholds as $threshold) {
			$this->runOne($threshold);
			$this->add_to_stats($this->lisa);
		}
	}
	
	function clear_stats() {
		$this->stats = array();
		$this->load_stats = array();
		$this->nos_cols = 0;
	}
	
	function add_to_stats($lisa) {
		$activation = $lisa->get_activation();
		$nos_loaded = $lisa->get_nos_loaded();
		$nos_triples = $lisa->get_nos_triples();

		$this->load_stats[] = array( 'loaded' => $nos_loaded, 'triples' => $nos_triples );
		
		foreach( $this->stats as $ent => &$cols ) {
			$cols[$this->nos_cols] = 0;
		}
		asort($activation);
		$activation = array_reverse($activation);  // highest first
		foreach ( $activation as $entity => $level ) {
			if ( !array_key_exists($entity,$this->stats) ) {
				if ( $this->nos_cols > 0 ) {
					$this->stats[$entity] = array_fill(0,$this->nos_cols,0);
				} else {
					$this->stats[$entity] = array();
				}
			}
			$this->stats[$entity][$this->nos_cols] = $level;
		}
		$this->nos_cols++;
	}

	function print_stats($include_headers=true,$delim="\t") {
		$str = "";
		if ( $include_headers ) {
			$thresholds_row = "threshold" . $delim . implode( $delim, $this->thresholds );
			$loaded .= "loaded";
			$triples .= "triples";
			foreach($this->load_stats as $load_stat) {
				$loaded .= $delim . $load_stat['loaded'];
				$triples .= $delim . $load_stat['triples'];
			}
			$str .= $thresholds_row . "\n";
			$str .= $loaded . "\n";
			$str .= $triples . "\n";
			$str .= "\n";
			$str .= $thresholds_row . "\n";
			$str .= "\n";
		}
		foreach( $this->stats as $ent => $cols ) {
			$str .= $ent;
			foreach($cols as $level) {
				$str .= $delim . $level;
			}
			$str .= "\n";
		}
		return $str;
	}
	

	
	function runOne($threshold) {
		
		echo "\n\n\n";
		echo "*************************************\n";
		echo "*************************************\n";
		echo "***  Threshold " . $threshold . " \n";
		echo "*************************************\n";
		echo "\n";
		
	
		// $this->lisa->set_nos_internal_cycles(1);
		$this->lisa->set_load_threshold($threshold); 
		$this->lisa->set_load_fanout_threshold($this->fanout_ratio * $threshold); 
		
		$this->lisa->clear();
	
		$this->lisa->activate($this->seed_entities);
	
		$this->lisa->iterate($this->nos_load_iterations);

	}
	
}


?>
